<?php
declare (strict_types = 1);

namespace app\common\job\command;

use app\common\service\CommonStoreManagerService;
use app\kefuapi\service\WorkermanService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class StoreManager extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('storeManager')
            ->addArgument('action', Argument::REQUIRED, 'start/stop/reload/status/connections')
            ->addOption('d', null, Option::VALUE_NONE, 'daemon（守护进程）方式启动')
            ->setDescription('商城-释放店长分销佣金');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action') ? : 'start';

        // 重新构造命令行参数,以便兼容workerman的命令
        global $argv;
        $argv = ['think', $action];

        if($input->hasOption('d')){
            $argv[] = '-d';
        }

        try {
            // 指令输出
            $output->writeln("store-manager {$action} success !");
            // 发放店长奖励
            $time = time();
            $lastMonthDate = date('Y-m', strtotime("-1 month", $time));
            CommonStoreManagerService::instance()->freeStoreManagerPrice($lastMonthDate);

        }catch (\Exception $e){
            $output->warning($e->getMessage());
        }

    }
}
